﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietPremier.Areas.AdminCP.Models;
using MvcLibrary.Repository;
using System.IO;

namespace VietPremier.Areas.AdminCP.Controllers
{
    public class AdminDestinationController : Controller
    {
        //
        // GET: /AdminCP/AdminDestination/

        public ActionResult Index(int id)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                ViewBag.IdCountry = id;
                var query = from p in db.EntityDestinations
                            join q in db.EntityDestinationCountries on p.IdDestinationCountry equals q.Id
                            where p.IdDestinationCountry == id
                            select new AdminDestinationModel
                            {
                                Id = p.Id,
                                IdDestinationCountry = p.IdDestinationCountry,
                                DestinationCountryname = q.Name,
                                Name = p.Name,
                                Image = p.Image,
                                Duration = p.Duration,
                                Description = p.Description,                                
                                DateCreate = p.DateCreate,
                                Count = p.Count,
                            };
                
                ViewBag.CountryName = db.EntityDestinationCountries.FirstOrDefault(p=>p.Id==id).Name;
                return View(query.ToList());
            }
        }

        public ActionResult Edit(int id)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                var query = from p in db.EntityDestinations
                            join q in db.EntityDestinationCountries on p.IdDestinationCountry equals q.Id
                            where p.Id == id
                            select new AdminDestinationModel
                            {
                                Id = p.Id,
                                IdDestinationCountry = p.IdDestinationCountry,                              
                                Name = p.Name,
                                Image = p.Image,                               
                                Description = p.Description,
                                Body = p.Body,
                                Duration = p.Duration,
                                DateCreate = p.DateCreate,
                                Count = p.Count,
                                Meta_description = p.Meta_description,
                                Meta_keyword=p.Meta_keyword,
                                Titletag=p.Titletag
                            };
                ViewBag.IdCountry = query.FirstOrDefault().IdDestinationCountry;
                return View(query.FirstOrDefault());
            }
        }


        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(AdminDestinationModel model, HttpPostedFileBase Image)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                EntityDestination Destination = db.EntityDestinations.FirstOrDefault(p => p.Id == model.Id);
                   if(Destination!=null)
                   {
                       Destination.IdDestinationCountry = model.IdDestinationCountry;
                    Destination.Name = model.Name;
                    Destination.Description = model.Description;
                    Destination.Body = model.Body;                
                    Destination.DateCreate = model.DateCreate;
                    Destination.Duration = model.Duration;
                    Destination.Count = model.Count;
                    //Destination.Hot = model.Hot;
                    //Destination.Spotlight = model.Spotlight;
                    Destination.Meta_keyword = model.Meta_keyword;
                    Destination.Meta_description = model.Meta_description;
                    Destination.Titletag = model.Titletag;
                    if (Image != null && Image.ContentLength > 0)
                    {
                        int kb = Image.ContentLength / 1024; //file size kb
                        if (kb >= 2048) // file qua lon
                        {
                            return RedirectToAction("Index", "Error", new { errorMsg = "Images must be less than 2MB." });
                        }
                        //delete file cu
                        if (Destination.Image != null)
                        {
                            string fileToDelete = Path.Combine(Server.MapPath("~/Content/upload/destination/"), Destination.Image); // file hinh cu
                            System.IO.File.Delete(fileToDelete);
                        }

                        ImageHelper imgHelper = new ImageHelper();
                        string encodestring = imgHelper.encodeImageFile(Image);

                        if (encodestring == "!")
                            return RedirectToAction("Index", "Error", new { errorMsg = "Can not upload image" });

                        var path = Path.Combine(Server.MapPath("~/Content/upload/destination/"), encodestring);
                        Image.SaveAs(path);

                        Destination.Image = encodestring;
                    }
                    db.SubmitChanges();

                    return RedirectToAction("Index", new { id = model.IdDestinationCountry });
                }
                else
                    return View("Error");
            }
        }

        public ActionResult Insert(int id)
        {
            ViewBag.IdCountry = id;
            return View();
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Insert(AdminDestinationModel model, HttpPostedFileBase Image, int IdCountry)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                EntityDestination Destination = new EntityDestination();
                Destination.IdDestinationCountry = IdCountry;
                Destination.Name = model.Name;
                Destination.Description = model.Description;
                Destination.Body = model.Body;              
                Destination.DateCreate = model.DateCreate;
                Destination.Duration = model.Duration;
                Destination.Count = 0;
                //Destination.Hot = model.Hot;
                //Destination.Spotlight = model.Spotlight;
                Destination.Meta_keyword = model.Meta_keyword;
                Destination.Meta_description = model.Meta_description;
                Destination.Titletag = model.Titletag;
                if (Image != null && Image.ContentLength > 0)
                {
                    int kb = Image.ContentLength / 1024; //file size kb
                    if (kb >= 2048) // file qua lon
                    {
                        return RedirectToAction("Index", "Error", new { errorMsg = "Images must be less than 2MB." });
                    }
                    //delete file cu
                    if (Destination.Image != null)
                    {
                        string fileToDelete = Path.Combine(Server.MapPath("~/Content/upload/destination/"), Destination.Image); // file hinh cu
                        System.IO.File.Delete(fileToDelete);
                    }

                    ImageHelper imgHelper = new ImageHelper();
                    string encodestring = imgHelper.encodeImageFile(Image);

                    if (encodestring == "!")
                        return RedirectToAction("Index", "Error", new { errorMsg = "Can not upload image" });

                    var path = Path.Combine(Server.MapPath("~/Content/upload/destination/"), encodestring);
                    Image.SaveAs(path);

                    Destination.Image = encodestring;
                }
                db.EntityDestinations.InsertOnSubmit(Destination);
                db.SubmitChanges();
                return RedirectToAction("Index", new { id = IdCountry });
            }
        }

        public string Delete(int id)
        {
            using (dbVietPremierDataContext db = new dbVietPremierDataContext())
            {
                var delquery = db.EntityImageDestinations.Where(p => p.IdDestination == id).ToList();
                db.EntityImageDestinations.DeleteAllOnSubmit(delquery);
                EntityDestination Destination = db.EntityDestinations.FirstOrDefault(p => p.Id == id);
                if (Destination != null)
                {
                    if (Destination.Image != null)
                    {
                        string fileToDelete = Path.Combine(Server.MapPath("~/Content/upload/destination/"), Destination.Image); // file hinh cu
                        System.IO.File.Delete(fileToDelete);
                    }
                    db.EntityDestinations.DeleteOnSubmit(Destination);
                    db.SubmitChanges();
                    return "Ok";
                }
                else
                    return "Error";
            }
        }
    }
}
